﻿using CreateDefect.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CreateDefect.Controllers
{
    public class DefectController : Controller
    {
        public ActionResult Index()
        {
            QCConnection connection = (QCConnection)Session["QCConnection"];

            ViewBag.Message = "Fill the fields below to submit a defect.";

            if (connection == null)
                return RedirectToAction("Index", "Login");
            else
            {
                if (connection.ProjectConnected == false)
                    return RedirectToAction("Index", "Login");
                else
                {
                    ViewBag.Domain = connection.ConnectedDomain;
                    ViewBag.Project = connection.ConnectedProject;
                }
            }

            // new list and a buffer item
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem()
            {
                Text = "",
                Selected = true
            });

            foreach (string s in connection.Projects(connection.ConnectedDomain))
                items.Add(new SelectListItem()
                {
                    Text = s
                });

            ViewBag.Projects = items;

            return View();
        }

        public ActionResult DefectFields(string project)
        {
            QCConnection connection = (QCConnection)Session["QCConnection"];
            if (project != connection.ConnectedProject)
            {
                string domain = connection.ConnectedDomain;
                connection.Disconnect();
                connection.Login(domain, project);
                Session["QCConnection"] = connection;
            }
            
            var requiredFields = connection.AllUserFields("BUG");

            List<Models.DefectField> fields = new List<Models.DefectField>();
            foreach (var a in requiredFields)
            {
                Models.DefectField field = new Models.DefectField()
                {
                    DisplayName = a,
                    ColumnName = connection.FieldColumnName("BUG", a),
                    DataType = connection.FieldType("BUG", a),
                    Fields = connection.FieldList("BUG", connection.FieldColumnName("BUG", a))
                };
                fields.Add(field);
            }

            ViewBag.Fields = fields;

            return PartialView();
        }

        public ActionResult Create(QCDefect UserDefect)
        {
            QCConnection connection = (QCConnection)Session["QCConnection"];
            QCDefect defect = (QCDefect)UserDefect;

            defect.Post();

            ViewBag.Reponse = "Defect " + defect.Id + " added";

            return RedirectToAction("Index");
        }

        //
        // POST: /Defect/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Defect/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Defect/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Defect/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Defect/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
